import { Cache } from "../../../cache/Cache";
import { DisplayUtil } from "../../../../common/DisplayUtil";
import { LayerManager } from "../../../manager/LayerManager";
import { Language } from "../../../../../extend/language/Language";
import { GameMapUtil } from "../../../scene/map/GameMapUtil";
import { RolePlayer } from "../../../scene/player/entity/RolePlayer";
import { Dispatcher } from "../../../../mvc/core/Dispatcher";
import { DataEvent } from "../../../events/DataEvent";
import { EventName } from "../../../mvc/EventName";
import { CrossDefenceCopyGuideItem } from "./item/CrossDefenceCopyGuideItem";
import { SystemSetter } from "../../system/SystemSetter";
//class CrossDefenceCopyGuider
    
    export  class CrossDefenceCopyGuider
    {
        

        public guideStep1(arg1: Object /* flash.geom.Point */): void
        {
            if (!Cache.instance.crossDefenceCopy.isGuiding) 
            {
                return;
            }
            this._lastTimerId = flash.utils.setTimeout(this.onStep1ShowTimeEnd, 6000);
            DisplayUtil.drawHollowMask(this.hollowMask, 40, 40);
            LayerManager.highestLayer.addChildAt(this.hollowMask, 0);
            this.guider.x = arg1.x;
            this.guider.y = arg1.y;
            this.hollowMask.x = this.guider.x + 32;
            this.hollowMask.y = this.guider.y + 140;
            LayerManager.highestLayer.addChild(this.guider);
            this.guider.update(Language.getString(80322), this.step1Finished, 40, 40);
            return;
        }

        public guideStep2(): void
        {
            if (!Cache.instance.crossDefenceCopy.isGuiding) 
            {
                return;
            }
            this._lastTimerId = flash.utils.setTimeout(this.onStep2GuideTimer, 6000);
            var loc1=GameMapUtil.getTilePoint(RolePlayer.instance.x, RolePlayer.instance.y);
            var loc2=this.getGuidePoint(loc1);
            var loc3=RolePlayer.instance.localToGlobal(new Object /* flash.geom.Point */(0, 0));
            this.guider.x = loc3.x - (loc1.x - loc2.x) * GameMapUtil.tileWidth - 48 - 42;
            this.guider.y = loc3.y - (loc1.y - loc2.y) * GameMapUtil.tileHeight - 161 - 50;
            this._guidePoint = loc2;
            DisplayUtil.drawHollowMask(this.hollowMask, 124, 124);
            LayerManager.highestLayer.addChildAt(this.hollowMask, 0);
            this.hollowMask.x = this.guider.x + 32;
            this.hollowMask.y = this.guider.y + 140;
            LayerManager.highestLayer.addChild(this.guider);
            this.guider.update(Language.getString(80323), this.step2Finished, 120, 120);
            return;
        }

        /* internal  */onStep2GuideTimer(): void
        {
            if (this._lastTimerId != 0) 
            {
                flash.utils.clearTimeout(this._lastTimerId);
            }
            Dispatcher.dispatchEvent(new DataEvent(EventName.CrossDefenceGuideStep2TimerEnd));
            this.hideGuide();
            return;
        }

        public hideGuide(): void
        {
            DisplayUtil.removeMe(this._guider);
            DisplayUtil.removeMe(this._hollowMask);
            return;
        }

        /* internal  */step1Finished(): void
        {
            if (this._lastTimerId != 0) 
            {
                flash.utils.clearTimeout(this._lastTimerId);
            }
            Dispatcher.dispatchEvent(new DataEvent(EventName.CrossDefenceGuideStep1Finished));
            this.hideGuide();
            this.guideStep2();
            return;
        }

        /* internal  */step2Finished(): void
        {
            if (this._lastTimerId != 0) 
            {
                flash.utils.clearTimeout(this._lastTimerId);
            }
            Dispatcher.dispatchEvent(new DataEvent(EventName.CrossDefenceGuideStep2TimerEnd));
            Dispatcher.dispatchEvent(new DataEvent(EventName.CrossDefenceGuideStep2Finished, this._guidePoint));
            this.hideGuide();
            return;
        }

        /* internal  */get guider(): CrossDefenceCopyGuideItem
        {
            if (this._guider == null) 
            {
                this._guider = new CrossDefenceCopyGuideItem();
            }
            return this._guider;
        }

        /* internal  */getGuidePoint(arg1: Object /* flash.geom.Point */): Object /* flash.geom.Point */
        {
            var loc4=null;
            var loc5=0;
            var loc6=0;
            var loc7=0;
            var loc1=100000000;
            var loc2=0;
            var loc3=0;
            while (loc3 < this.playerPoints.length) 
            {
                loc4 = this.playerPoints[loc3];
                loc5 = arg1.x - loc4.x;
                loc6 = arg1.y - loc4.y;
                if ((loc7 = loc5 * loc5 + loc6 * loc6) < loc1) 
                {
                    loc1 = loc7;
                    loc2 = loc3;
                }
                ++loc3;
            }
            return this.guidePoints[loc2] as Object /* flash.geom.Point */;
        }

        /* internal  */get hollowMask(): Object /* flash.display.Sprite */
        {
            if (this._hollowMask == null) 
            {
                this._hollowMask = new Object /* flash.display.Sprite */();
            }
            return this._hollowMask;
        }

        /* internal  */onStep1ShowTimeEnd(): void
        {
            if (this._lastTimerId != 0) 
            {
                flash.utils.clearTimeout(this._lastTimerId);
                this._lastTimerId = 0;
            }
            SystemSetter.systemGuideSetterInfo.isCrossDefenceCopyGuided = true;
            Dispatcher.dispatchEvent(new DataEvent(EventName.SaveGuideSystemSetter));
            this.hideGuide();
            return;
        }

        /* internal */ /* const */guidePoints: Array<any>=[new Object /* flash.geom.Point */(119, 54), new Object /* flash.geom.Point */(164, 258), new Object /* flash.geom.Point */(136, 322), new Object /* flash.geom.Point */(43, 323), new Object /* flash.geom.Point */(18, 114)];

        /* internal */ /* const */playerPoints: Array<any>=[new Object /* flash.geom.Point */(123, 43), new Object /* flash.geom.Point */(169, 258), new Object /* flash.geom.Point */(136, 333), new Object /* flash.geom.Point */(42, 334), new Object /* flash.geom.Point */(13, 115)];

        /* internal */ /* var */_guider: CrossDefenceCopyGuideItem;

        /* internal */ /* var */_guidePoint: Object /* flash.geom.Point */;

        /* internal */ /* var */_hollowMask: Object /* flash.display.Sprite */;

        /* internal */ /* var */_lastTimerId: uint=0;
    }
